import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.text.SimpleDateFormat;

public class UserManager {
    private static List<User> users = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    static {
        // 初始化管理员
        users.add(new User("1", "admin", "ynuinfo#777", "admin", "13800138000", new Date()));
    }

    // 登录功能
    public User login(String username, String password) {
        for (User user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }
    public User findUserById(String id) {
        for (User user : users) {
            if (user.getUserId().equals(id)) {
                return user;
            }
        }
        return null;
    }

    // 列出所有用户
    public void listAllUsers() {
        System.out.println("\n用户列表:");
        for (User user : users) {
            System.out.println("ID: " + user.getUserId() +
                    " 用户名: " + user.getUsername() +
                    " 类型: " + user.getUserType() +
                    " 手机: " + user.getPhone());
        }
    }

    // 查找用户
    public User findUserByUsername(String username) {
        for (User user : users) {
            if (user.getUsername().equals(username)) {
                return user;
            }
        }
        return null;
    }

    // 删除用户
    public void deleteUser(User admin, String username) {
        if (username.equals("admin")) {
            System.out.println("不能删除admin账户");
            return;
        }

        User user = findUserByUsername(username);
        if (user != null) {
            System.out.print("确定删除? (y/n): ");
            if (scanner.nextLine().equals("y")) {
                users.remove(user);
                System.out.println("删除成功");
                logOperation(admin.getUsername(), "删除用户" + username, new Date());
            }
        } else {
            System.out.println("用户不存在");
        }
    }

    // 增加用户
    public void addUser(User admin, String userId, String username, String type, String phone) {
        if (findUserByUsername(username) != null) {
            System.out.println("用户名已存在");
            return;
        }

        User newUser = new User(userId, username, "123456", type, phone, new Date());
        users.add(newUser);
        System.out.println("用户添加成功，默认密码123456");
        logOperation(admin.getUsername(), "添加" + type + "用户" + username, new Date());
    }

    // 修改用户信息
    public void modifyUserInfo(User admin, String username) {
        User user = findUserByUsername(username);
        if (user == null || user.getUserType().equals("admin")) {
            System.out.println("用户不存在或为管理员，无法修改");
            return;
        }

        System.out.println("1. 修改用户类型");
        System.out.println("2. 修改手机号");
        System.out.print("选择操作: ");
        int choice = getValidIntegerInput();

        if (choice == 1) {
            System.out.print("请输入新类型(manager/receptionist): ");
            String newType = scanner.nextLine();
            if (newType.equals("manager") || newType.equals("receptionist")) {
                user.setUserType(newType);
                System.out.println("用户类型修改成功");
                logOperation(admin.getUsername(), "修改用户" + username + "类型为" + newType, new Date());
            } else {
                System.out.println("无效用户类型");
            }
        } else if (choice == 2) {
            System.out.print("请输入新手机号: ");
            String newPhone = scanner.nextLine();
            user.setPhone(newPhone);
            System.out.println("手机号修改成功");
            logOperation(admin.getUsername(), "修改用户" + username + "手机号", new Date());
        }
    }

    // 管理员修改自身密码
    public void manageAdminPassword(User admin) {
        System.out.println("\n1. 修改自身密码");
        System.out.println("2. 重置非管理员密码");
        System.out.print("选择操作: ");
        int choice = getValidIntegerInput();

        switch (choice) {
            case 1:
                System.out.print("请输入新密码: ");
                String newPwd = scanner.nextLine();
                admin.setPassword(newPwd);
                System.out.println("密码修改成功");
                logOperation(admin.getUsername(), "修改自身密码", new Date());
                break;
            case 2:
                System.out.print("请输入要重置密码的用户名: ");
                String username = scanner.nextLine();
                User user = findUserByUsername(username);
                if (user != null &&!user.getUserType().equals("admin")) {
                    user.setPassword("123456");
                    System.out.println("密码已重置为123456，用户登录时需修改");
                    logOperation(admin.getUsername(), "重置" + user.getUserType() + "密码", new Date());
                } else {
                    System.out.println("用户不存在或为管理员");
                }
                break;
            default:
                System.out.println("无效选择");
        }
    }

    // 经理修改自身密码和重置前台密码
    public void manageManagerPassword(User manager) {
        System.out.println("\n1. 修改自身密码");
        System.out.println("2. 重置前台用户密码");
        System.out.print("选择操作: ");
        int choice = getValidIntegerInput();

        switch (choice) {
            case 1:
                System.out.print("请输入新密码: ");
                String newPwd = scanner.nextLine();
                manager.setPassword(newPwd);
                System.out.println("密码修改成功");
                logOperation(manager.getUsername(), "修改自身密码", new Date());
                break;
            case 2:
                System.out.print("请输入要重置密码的前台用户名: ");
                String username = scanner.nextLine();
                User user = findUserByUsername(username);
                if (user != null && user.getUserType().equals("receptionist")) {
                    user.setPassword("123456");
                    System.out.println("密码已重置为123456，用户登录时需修改");
                    logOperation(manager.getUsername(), "重置前台" + username + "密码", new Date());
                } else {
                    System.out.println("用户不存在或不是前台");
                }
                break;
            default:
                System.out.println("无效选择");
        }
    }

    private int getValidIntegerInput() {
        while (true) {
            try {
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的整数");
            }
        }
    }

    private void logOperation(String operator, String action, Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String log = sdf.format(time) + " - 操作人: " + operator + " - 操作: " + action;
        System.out.println("[日志] " + log);
    }
}
